package com.tongtong.controller.email.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tongtong.common.WebConstants;
import com.tongtong.controller.email.entity.TtEmailConfig;
import com.tongtong.controller.email.entity.TtEmailContent;
import com.tongtong.controller.email.service.TtEmailConfigService;
import com.tongtong.controller.email.service.TtEmailContentService;
import com.tongtong.controller.email.utils.SendEmailUtils;
import com.tongtong.controller.email.vo.EmailRequest;
import com.tongtong.utils.RedisUtils;
import com.tt.utils.R;
import com.tt.utils.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/content")
public class EmailContentController {
    @Autowired
    private TtEmailContentService ttEmailContentService;
    @Autowired
    private TtEmailConfigService ttEmailConfigService;

    @GetMapping("/getEmailContent")
    public R getEmailContent(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,
                             @RequestParam(value = "name") String name, @RequestParam(value = "status", required = false) String status) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(name)) {
            queryWrapper.eq("email", name);
        }
        if (StrUtil.isNotEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByDesc("update_time");
        List<TtEmailContent> list = ttEmailContentService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return R.ok().put("data", pageInfo);
    }

    @PostMapping("/deleteEmailContent/{id}")
    public R deleteEmailContent(@PathVariable("id") Integer id) {
        TtEmailContent ttEmailContent = new TtEmailContent();
        ttEmailContent.setId(id);
        ttEmailContent.setDeleted(1);
        ttEmailContentService.updateById(ttEmailContent);
        return R.ok();
    }

    @PostMapping("/deleteEmailContents")
    public R deleteEmailContents(@RequestBody List<Integer> ids) {
        TtEmailContent ttEmailContent = new TtEmailContent();
        ttEmailContent.setDeleted(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        ttEmailContentService.update(ttEmailContent, queryWrapper);
        return R.ok();
    }

    @PostMapping("/sendEmail")
    public R sendEmail(@RequestBody @Validated EmailRequest.sendEmail sendEmail) {
        TtEmailConfig emailConfig = ttEmailConfigService.getById(12);
        if(Objects.isNull(emailConfig)){
            throw new RuntimeException("发送方查询失败");
        }
        SendEmailUtils sendEmailUtils = new SendEmailUtils();
        SendEmailUtils.SendEmail sendEmails = new SendEmailUtils.SendEmail();
        sendEmails.setFrom(emailConfig.getEmail());
        sendEmails.setTo(Arrays.asList(sendEmail.getToEmail()));
        sendEmails.setTitle(sendEmail.getSubject());
        String randomCode = VerificationCodeUtil.generateRandomCode();
        String format = String.format("【木木哒】邮箱验证码：【%s】,有效期为10分钟", randomCode);
        sendEmails.setContent(format);
        boolean b = sendEmailUtils.sendMultipleEmail(sendEmails, emailConfig);
        RedisUtils.set(WebConstants.redisEmailCodeKey+sendEmail.getToEmail(), randomCode, 10, TimeUnit.MINUTES);
        return R.ok().put("data",b);
    }


    @GetMapping("/ceshi")
    public R sendEmail() {
        TtEmailConfig byId = ttEmailConfigService.getById(12);
        SendEmailUtils sendEmailUtils2 = new SendEmailUtils();
        SendEmailUtils.SendEmail sendEmail = new SendEmailUtils.SendEmail();
        sendEmail.setFrom(byId.getEmail());
        sendEmail.setTo(Arrays.asList("mmdt2021@126.com"));
        sendEmail.setTitle("asdf");
        sendEmail.setContent("ceshi");
        Map<String, String> imgs = new HashMap<>();
        imgs.put("asdf", "E:\\image\\2024-09-25\\9da0b1229259477fb8f24785c11b5c08.jpg");
        sendEmail.setImags(imgs);
//        Map<String, String> files = new HashMap<>();
//        files.put("Earth.zip","E:\\webrtc.io-demo-master.zip");
//        sendEmail.setFiles(files);
//        sendEmail.setContent(sendEmail.getContent()+byId.getSign());
        sendEmailUtils2.sendMultipleEmail(sendEmail, byId);
        return R.ok();
    }


}
